﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Interface;
using Model;

namespace DAL.Dao
{
    public class ManagerDaoimpl : Base, IManagerDao
    {
        public int ExistNum { get { return DbEntity.expressDBEntities.Manager.Count(); } private set { } }

        public ManagerDaoimpl()
        {
              DbEntity.CheckConn();
        }
        public IList<Manager> GetList()
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.Manager select i).ToList();
        }

        public (int,IList<Manager>) GetList(int rows, int page)
        {
            DbEntity.CheckConn();
            var rs = DbEntity.expressDBEntities.Manager.OrderBy(o => o.ID).ToList();
            return (GetTotalPages(rs.Count, rows), rs.Skip((page - 1) * rows).Take(rows).ToList());
        }



        public Manager GetItemByID(int id)
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.Manager
                    where i.ID == id
                    select i).ToList().FirstOrDefault();
        }

        public bool Update(int id, Manager newitem)
        {

            DbEntity.CheckConn();
            Manager item = (from i in DbEntity.expressDBEntities.Manager
                                where i.ID == id
                                select i).ToList().FirstOrDefault();
            if (item == null)
            {
                return false;
            }
            //DbEntityEntry<Bag> entry = DbEntity.expressDBEntities.Entry<Bag>(newitem);
            //3.设置该对象为被修改过
            //entry.State = EntityState.Unchanged;
            //4.设置该对象的 uName属性为修改状态，同时 entry.State 被修改为 Modified 状态
            item.UserName = newitem.UserName;
            item.UserPass = newitem.UserPass;
            item.TrueName = newitem.TrueName;
            item.Phone = newitem.Phone;
            item.IDCard = newitem.IDCard;
            item.UpdateTime = newitem.UpdateTime;
            item.AddTime = newitem.AddTime;


            DbEntity.expressDBEntities.SaveChanges();

            return true;
        }

        public Manager Find(int id)
        {
            DbEntity.CheckConn();
            return DbEntity.expressDBEntities.Manager.Find(id);
        }

        public bool Delete(int id)
        {
            DbEntity.CheckConn();
            Manager manager = DbEntity.expressDBEntities.Manager.Find(id);
            if (manager == null) return false;
            DbEntity.expressDBEntities.Manager.Remove(manager);
            DbEntity.expressDBEntities.SaveChanges();
            return true;
        }
   

        public Manager Login(string username, string userpass)
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.Manager
                                  where i.UserName.Trim().Equals(username) && i.UserPass.Trim().Equals(userpass)
                                   select i).ToList().FirstOrDefault(); 
            
        }


        public bool Add(Manager manager)
        {
            DbEntity.CheckConn();
            manager.ID = GetList().Last().ID + 1;
            DbEntity.expressDBEntities.Manager.Add(manager);
            DbEntity.expressDBEntities.SaveChanges();
            return true;
        }
        public void Dispose()
        {
            if (DbEntity.expressDBEntities != null) DbEntity.expressDBEntities.Database.Connection.Close();
        }

    }
}
